package gin.dev.update;

import com.appvn2014.appstorevn.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.vstore.activities.SceneLoadingActivity;

public class DownloadNotification {

	public static String TAG = "Download test";
	public static int NOTIFY_ID_MAX = 10500;
	public static int NOTIFY_ID_MIN = 10000;
	public static int NOTIFY_ID = NOTIFY_ID_MIN;
	
	public static void HotAppNotify(String appname, int productID, Context context)
	{
		NOTIFY_ID++;
		if(NOTIFY_ID > NOTIFY_ID_MAX)
			NOTIFY_ID = NOTIFY_ID_MIN;
    
	    Intent i;

        i = new Intent(context, SceneLoadingActivity.class);
        i.putExtra("productID", productID);
        
        PendingIntent pendInt = PendingIntent.getActivity(context, NOTIFY_ID, i,  PendingIntent.FLAG_ONE_SHOT);
        Notification notif = new Notification(R.drawable.ic_launcher,context.getText(R.string.vstore),System.currentTimeMillis());
        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notif.setLatestEventInfo(context, context.getText(R.string.vstore), appname, pendInt);
        
        notif.defaults |= Notification.FLAG_SHOW_LIGHTS;
        notif.defaults |= Notification.DEFAULT_SOUND;
        notif.defaults |= Notification.DEFAULT_VIBRATE;
        notif.defaults |= Notification.DEFAULT_LIGHTS;
        notif.flags |= Notification.FLAG_AUTO_CANCEL;    
        //i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //notif.flags |= Notification.FLAG_ONGOING_EVENT;
        
        notif.ledARGB = 0xff00ff00;
        notif.ledOnMS = 300;
        notif.ledOffMS = 1000; 
        nm.notify(NOTIFY_ID, notif);
	}
	public static void Notify(String appname, int quereID, Context context)
    {
		final int taskID = quereID;
		NOTIFY_ID++;
		if(NOTIFY_ID > NOTIFY_ID_MAX)
			NOTIFY_ID = NOTIFY_ID_MIN;
    	final int id = NOTIFY_ID;
    	
    	Intent intent = new Intent(context, SceneLoadingActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
        
        int productID = DownloadQuere.mProductIDQuere.get(quereID);
        intent.putExtra("DownloadTaskID", quereID);
        intent.putExtra("NotificationID", id);
        intent.putExtra("productID", productID);
        
    	final NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    	final android.support.v4.app.NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    	mBuilder.setContentTitle(appname)
    	    .setContentText("Download in progress")
    		.setSmallIcon(R.drawable.ic_launcher)
    		.addAction(0, "Check", pIntent);
    	// Start a lengthy operation in a background thread
    	new Thread(
    	    new Runnable() {
    	        @Override
    	        public void run() {
    	        	
    	        	DownloadTask task = DownloadQuere.mTaskQuere.get(taskID);
    	            while (task.progressValue >= 0)
    	            {
    	            	mBuilder.setProgress(100, task.progressValue, false);
    	            	mNotifyManager.notify(id, mBuilder.build());
                        try {
                            // Sleep for 1 seconds
                            Thread.sleep(1*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
    	            }
    	            // When the loop is finished, updates the notification
    	            if(task.progressValue == DownloadTask.DOWNLOAD_CANCEL)
    	            	mBuilder.setContentText("Download Canceled").setProgress(0,0,false);
    	            if(task.progressValue == DownloadTask.DOWNLOAD_COMPLETE)
    	            	mBuilder.setContentText("Download Complete").setProgress(0,0,false);
    	            if(task.progressValue == DownloadTask.DOWNLOAD_ERROR)
    	            	mBuilder.setContentText("Download Error").setProgress(0,0,false);
    	            mNotifyManager.notify(id, mBuilder.build());
    	        }
    	    }
    	// Starts the thread by calling the run() method in its Runnable
    	).start();
    }
	
	public static void CancelNotification(Context context, int id)
	{
		String ns = Context.NOTIFICATION_SERVICE;
	    NotificationManager nMgr = (NotificationManager) context.getSystemService(ns);
	    nMgr.cancel(id);
	}
}
